package dao;

import help.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.TestDTO;

public class TestDAO extends DatabaseHelper {
	
    public static final String TABLE_TEST = "Test";
    
    public static final String TEST_ID = "ID";
    public static final String TEST_TESTDATE = "TestDate";
    public static final String TEST_DOCTORID = "DoctorID";
    public static final String TEST_PATIENTID = "PatientID";
    
	public TestDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public int addTest(TestDTO testDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TEST_TESTDATE, testDTO.getTestDate().toString());
	        values.put(TEST_DOCTORID, testDTO.getDoctorID());
	        values.put(TEST_PATIENTID, testDTO.getPatientID());
	        
	        // Inserting rows
	        open();
	        long id = DatabaseHelper.myDataBase.insert(TABLE_TEST, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
	        return (int)id;
        }
        catch (Exception ex) { return -1;}
    }         
    
    public TestDTO getTest(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_TEST, new String[] { TEST_ID,
	        		TEST_TESTDATE, TEST_DOCTORID, TEST_PATIENTID }, TEST_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        TestDTO test = new TestDTO(
	                Integer.parseInt(cursor.getString(0)),
	                new Date(cursor.getString(1)),
	                Integer.parseInt(cursor.getString(2)),
	                Integer.parseInt(cursor.getString(3)));
	        return test;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<TestDTO> getAllTests() {
    	try {
	        List<TestDTO> testList = new ArrayList<TestDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_TEST;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do{
	            	TestDTO test = new TestDTO(
	    	                Integer.parseInt(cursor.getString(0)),
	    	                new Date(cursor.getString(1)),
	    	                Integer.parseInt(cursor.getString(2)),
	    	                Integer.parseInt(cursor.getString(3)));
	            
	            	testList.add(test);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return testList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateTest(TestDTO testDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(TEST_ID, testDTO.getID());
	        values.put(TEST_TESTDATE, testDTO.getTestDate().toString());
	        values.put(TEST_DOCTORID, testDTO.getDoctorID());
	        values.put(TEST_PATIENTID, testDTO.getPatientID());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_TEST, values, TEST_ID + " = ?",
	                new String[] { String.valueOf(testDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { return false; }
    }
    
    public void deleteTest(TestDTO testDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TEST, TEST_ID + " = ?",
                new String[] { String.valueOf(testDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllTests() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_TEST, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getTestCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_TEST;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}
